<?php
/**
 * 销售系统订单详细
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/statistic.php');

$smarty->assign('lang', $_LANG);

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'detail';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

//main
if ($_REQUEST['act'] == 'detail')
{
	admin_priv('sale_order_stats');


	$order_id = ( !empty($_REQUEST['order_id']) )? $_REQUEST['order_id'] : 0;
	$summary = array();
	$detail = array();
    if($order_id!= 0){
        $res = order_detail($order_id);
        $summary = $res['summary'];
        $detail  = $res['detail'];
        
    }

	$smarty->assign('order_general',       $order_general);
	$smarty->assign('ur_here',             "订单详情");
	$smarty->assign('detail',              $detail);
    $smarty->assign('summary',             $summary);
	$smarty->display('order_detail.htm');
}

function order_detail($order_id='')
{
    //各地区的邮费
    include_once('includes/shipping_fee_rule.php');

    $sql = "SELECT gl.order_id, gl.order_sn, gl.add_time, gl.province, gl.agency_id, gl.pay_id, gl.shipping_fee, g.goods_sku, g.goods_name, g.goods_weight AS weight, g.shop_price, gs.goods_xprice AS xprice ,gl.goods_number, gl.goods_price" .
           " FROM ( SELECT oi.order_sn ,oi.order_id, oi.add_time, oi.province, oi.agency_id, oi.pay_id, oi.shipping_fee, og.goods_sku, og.goods_number, og.goods_price" . 
                    " FROM egg_order_info AS oi LEFT JOIN egg_order_goods AS og ON oi.order_id=og.order_id " . 
                    " WHERE oi.order_id=$order_id AND order_status != 2 AND order_status != 3 AND (pay_id = 3 OR (pay_id = 1 AND pay_status = 2)) ".
           ") AS gl " .
           " LEFT JOIN egg_goods AS g ON gl.goods_sku=g.goods_sku " .
           " LEFT JOIN egg_goods_stock AS gs ON gl.goods_sku=gs.goods_sku and gl.agency_id=gs.area_id";

    $res = $GLOBALS['db']->query($sql);
    $order['summary'] = array();  //订单概览
    $order['detail']  = array();  //订单商品详情
    $i = 0;
    while ( $row = $GLOBALS['db']->fetchRow($res) ) {
        $order['summary']['order_id'] = $row['order_id'];
    	$order['summary']['order_sn'] = $row['order_sn'];
    	$order['summary']['add_time'] = $row['add_time'];
    	$order['summary']['province'] = $row['province'];
        $order['summary']['agency_id'] = $row['agency_id'];
    	$order['summary']['pay_id']   = $row['pay_id'];
        $order['summary']['all_weight']  += $row['weight'] * $row['goods_number'];
        $order['summary']['xprice']      += $row['xprice'] * $row['goods_number'];
        $order['summary']['shop_price']  += $row['shop_price'] * $row['goods_number'];   //售价
        $order['summary']['goods_price'] += $row['goods_price'] * $row['goods_number'];  //实售价
        $order['summary']['shipping_fee'] = $row['shipping_fee'];  //客户支付邮费

    	$order['detail'][$i]['sku']            = $row['goods_sku'];
    	$order['detail'][$i]['name']           = $row['goods_name'];
    	$order['detail'][$i]['num']            = $row['goods_number'];
    	$order['detail'][$i]['weight']         = $row['weight'];
    	$order['detail'][$i]['xprice']         = $row['xprice'];
    	$order['detail'][$i]['shop_price']     = $row['shop_price'];  
        $order['detail'][$i++]['goods_price']  = $row['goods_price'];  	
  	}
    $order['summary']['pay']  =  $order['summary']['goods_price'] + $order['summary']['shipping_fee'];  //支付
    $order['summary']['add_time'] = date('Y-m-d H:i:s', $order['summary']['add_time']);
    $order['summary']['province_name'] = selectaddress($order['summary']['province']);
    $order['summary']['package_fee']   = $order['summary']['shop_price'] * 0.0087 + 5.087;   //打包成本
    /* 计算邮费 */
    $real_weight = $order['summary']['all_weight']; //重量
    $base_weight = ($order['summary']['province'] == 2132) ? 5 : 1;     //如果是广东，则首重是5斤
    if($real_weight <= $base_weight){
        $order['summary']['shipping_fee'] = $shipping[$order['summary']['province']]['skg'];
    }else{
        $order['summary']['shipping_fee'] = $shipping[$order['summary']['province']]['skg'] + $shipping[$order['summary']['province']]['xkg']*ceil($real_weight-$base_weight);
    }
    $order['summary']['pay_way'] = '支付宝支付';  //默认支付宝
    //判断是否到付
    if($order['summary']['pay_id'] == 3){
        $order['summary']['pay_way'] = '货到付款';
        
        $extra = 0;
        if($order['summary']['agency_id'] == 1){
            $extra = 3;
        }else if($order['summary']['agency_id'] == 2 && $order['summary']['order_amount'] < 335){
            $extra = 5;
        }else if($order['summary']['agency_id'] == 2 && $order['summary']['order_amount'] >= 335){
            $extra = $order['summary']['order_amount'] * 0.015;
        }
        $order['summary']['shipping_fee'] += $extra;
    }
    
    //纯利润
    $order['summary']['pure'] = $order['summary']['pay']-$order['summary']['xprice']-$order['summary']['shipping_fee']-$order['summary']['package_fee'];
 	
    return $order;   
}
?>